package pumpkin.ast;

import pumpkin.evt.Environment;
import pumpkin.word.TokenSource;
/**
 * E->TE'
 * @author jiang
 *
 */
public class Expr implements Node{
	Texpr texpr=null;
	Exprp exprp=null;
	public void build(TokenSource tokenSource) {
		texpr=new Texpr();
		exprp=new Exprp();
		texpr.build(tokenSource);
		exprp.build(tokenSource);
	}

	public Object excute(Environment env) {
		Object left=texpr.excute(env);
		Object right=exprp.excute(env);
		char op=exprp.getOerator();
		if(left==null) {
			return right;
		}
		if(right==null) {
			return left;
		}
		if(op=='+') {
			if (left instanceof Double || right instanceof Double) {
				return (Double.parseDouble(left.toString()) + Double.parseDouble(right.toString()));
			} else {
				return (Integer.parseInt(left.toString()) + Integer.parseInt(right.toString()));
			}
		}else if(op=='-') {
			if (left instanceof Double || right instanceof Double) {
				return (Double.parseDouble(left.toString()) - Double.parseDouble(right.toString()));
			} else {
				return (Integer.parseInt(left.toString()) - Integer.parseInt(right.toString()));
			}
		}else {
			return left;
		}
	}

}
